const express = require('express');
const req = require('request');
const app = express();

app.get('/',(request, response) => {
    response.sendFile(__dirname + '/kuaidi.html');
});

//查询快递的路由规则
app.get('/query', (request, response) => {
    //获取用户要查询的运单编号
    let code = request.query.code;
    //查询快递 100
    const options = {
        url: 'https://www.kuaidi100.com/query?type=yuantong&postid='+code+'&temp=0.4558136110720501&phone=',
        headers: {
            'User-Agent': 'request',
            'Host': 'www.kuaidi100.com',
            'Connection': 'keep-alive',
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74',
            'X-Requested-With': 'XMLHttpRequest',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Referer': 'https://www.kuaidi100.com/?from=openv',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        }
    };
    req(options, function(err, res, body){
        if(err) {
            //请求错误
            response.json({
                code: 1,
                msg: '接口调用失败',
            })
        }else{
            response.json({
                code: 0,
                msg: '调用成功',
                data: JSON.parse(body)
            })
        }
    });
});

app.listen(80, () => {
    console.log('服务已经启动....');
});